package it.blueocean.acanto.taglibrary.jsp.taglib.tags.utils;

import java.io.IOException;
import javax.servlet.jsp.JspWriter;

public class HtmlPrintHelper
{
	public static void printOpenedHtmlStartTag(String paramString1, String paramString2, String paramString3,
			JspWriter paramJspWriter) throws IOException
	{
		paramJspWriter.print('<');
		paramJspWriter.print(paramString1);
		if(paramString2 != null)
			printAttribute("class", paramString2, paramJspWriter);
		if(paramString3 == null)
			return;
		printAttribute("style", paramString3, paramJspWriter);
	}

	public static void printHtmlStartTag(String paramString1, String paramString2, String paramString3,
			JspWriter paramJspWriter) throws IOException
	{
		printOpenedHtmlStartTag(paramString1, paramString2, paramString3, paramJspWriter);
		printClosedHtmlStartTag(paramJspWriter);
	}

	public static void printAttribute(String paramString1, String paramString2, JspWriter paramJspWriter)
			throws IOException
	{
		if(paramString2 == null)
			return;
		paramJspWriter.print(' ');
		paramJspWriter.print(paramString1);
		paramJspWriter.print("=\"");
		paramJspWriter.print(HTMLEncode(paramString2));
		paramJspWriter.print('"');
	}

	private static String HTMLEncode(String paramString)
	{
		StringBuffer localStringBuffer = new StringBuffer("");
		paramString = sobstitute(paramString, "&", "&amp;");
		paramString = sobstitute(paramString, "<", "&lt;");
		paramString = sobstitute(paramString, "\"", "&quot;");
		for (int i = 0; (paramString != null) && (i < paramString.length()); ++i)
		{
			int j = paramString.charAt(i);
			if(paramString.charAt(i) > '')
				localStringBuffer.append("&#" + j + ";");
			else
				localStringBuffer.append(paramString.charAt(i));
		}
		return localStringBuffer.toString();
	}

	private static String sobstitute(String paramString1, String paramString2, String paramString3)
	{
		for (int i = paramString1.indexOf(paramString2, 0); i != -1; i = paramString1.indexOf(paramString2, i
				+ paramString3.length()))
			paramString1 = paramString1.substring(0, i) + paramString3
					+ paramString1.substring(i + paramString2.length(), paramString1.length());
		return paramString1;
	}

	public static void printClosedHtmlStartTag(JspWriter paramJspWriter) throws IOException
	{
		paramJspWriter.print('>');
	}

	public static void printClosedHtmlEmptyTag(JspWriter paramJspWriter) throws IOException
	{
		paramJspWriter.print("/>");
	}

	public static void printHtmlEndTag(String paramString, JspWriter paramJspWriter) throws IOException
	{
		paramJspWriter.print("</");
		paramJspWriter.print(paramString);
		paramJspWriter.print('>');
	}

	public static void printHtmlEmptyTag(String paramString, JspWriter paramJspWriter) throws IOException
	{
		paramJspWriter.print("<");
		paramJspWriter.print(paramString);
		paramJspWriter.print("/>");
	}
}
